package com.coocaa.internship.common;

import lombok.Builder;

/**
 * @Description: 返回前端数据封装类
 * @Author: zwh
 * @Date: 2019-07-27
 * @Time: 15:08
 */
@Builder
public class ResultData<T> {
    private Integer code;

    private String message;

    private T data;

    public ResultData() {
    }

    public ResultData(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    public static ResultData success(Object data) {
        return ResultData.builder().code(ResultCode.SUCCESS.getCode())
                .message(ResultCode.SUCCESS.getMessage())
                .data(data)
                .build();
    }

    public static ResultData failed() {
        return ResultData.builder().code(ResultCode.FAILED.getCode())
                .message(ResultCode.FAILED.getMessage())
                .build();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"code\":")
                .append(code);
        sb.append(",\"message\":\"")
                .append(message).append('\"');
        sb.append(",\"data\":")
                .append(data);
        sb.append('}');
        return sb.toString();
    }
}
